<?php

/**
 * Global validator class
 */

class WordPress_PDK_Validators
{
	/**
	 * @var string - validator type
	 */
	private $_type = '';
	
	
	/**
	 * @var mixed - the value to be validated
	 */
	private $_value = null;
	
	
	/**
	 * @var array - validator parameters
	 */
	private $_params = array();
	
	
	/**
	 * @var string - the error message
	 */
	public $errorMessage = '';
	
	
	/**
	 * Constructor
	 *
	 * @param string the validator
	 * @return null
	 */
	public function __construct( $type = '', $value = null, $params = array() )
	{
		$this->_type = $type;
		$this->_value = is_string($value) ? trim($value) : $value;
		$this->_params = $params;
	}
	
	
	/**
	 * Check validation
	 *
	 * @return boolean
	 */
	public function pass()
	{
		switch($this->_type) {
			case 'required' : return $this->_required(); break;
			case 'length' : return $this->_length(); break;
		}
	}
	
	
	/**
	 * check length
	 *
	 * @return boolean
	 */
	private function _length()
	{
		$length = strlen($this->_value);
		if(isset($this->_params['min']) && ($length < $this->_params['min'])) {
			$this->errorMessage = isset($this->_params['message']) ? $this->_params['message'] : 'This attribute needs to be a minimum of ' . $this->_params['min'] . ' characters';
			return false;
		}
		
		if(isset($this->_params['max']) && ($length > $this->_params['max'])) {
			$this->errorMessage = isset($this->_params['message']) ? $this->_params['message'] : 'This attribute cannot exceed ' . $this->_params['max'] . ' characters';
			return false;
		}
		
		return true;
	}
	
	
	/**
	 * Required
	 *
	 * @return boolean
	 */
	private function _required()
	{
		if(empty($this->_value)) {
			$this->errorMessage = ' is required';
			return false;
		}

		return true;
	}
	
}

?>